package hello;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class Client {

    private String Name;
    
    void SetName(String name) {
        this.Name = name;
    }

    String StartGame() {

        HttpConnection c = null;
        InputStream is = null;
        StringBuffer sb = new StringBuffer();

        try {
            c = (HttpConnection) Connector.open(
                    "http://localhost:8080/WordPuzzleServer/GameManagerServlet?cmd=startGame");

            c.setRequestMethod(HttpConnection.GET);

            try {
                is = c.openInputStream();
            } catch (IOException x) {
                x.printStackTrace();
            }

            int ch = 0;

           
            for (int ccnt = 0; ccnt < 150; ccnt++) {
                ch = is.read();
                if (ch == -1) {
                    break;
                }
                sb.append((char) ch);
            }


        } catch (IOException x) {
            x.printStackTrace();
        } finally {
            try {
                is.close();
                c.close();
            } catch (IOException x) {
                x.printStackTrace();
            }
        }
       
        return sb.toString();
    }

    String ListGames() {

        HttpConnection c = null;
        InputStream is = null;
        StringBuffer sb = new StringBuffer();

        try {
            c = (HttpConnection) Connector.open(
                    "http://localhost:8080/WordPuzzleServer/GameManagerServlet?cmd=listGames");

            c.setRequestMethod(HttpConnection.GET);

            try {
                is = c.openInputStream();
            } catch (IOException x) {
                x.printStackTrace();
            }

            int ch = 0;


            for (int ccnt = 0; ccnt < 150; ccnt++) {
                ch = is.read();
                if (ch == -1) {
                    break;
                }
                sb.append((char) ch);
            }


        } catch (IOException x) {
            x.printStackTrace();
        } finally {
            try {
                is.close();
                c.close();
            } catch (IOException x) {
                x.printStackTrace();
            }
        }

        return sb.toString();
    }

    void StopGame(String id) {

        HttpConnection c = null;
        InputStream is = null;

        try {
            c = (HttpConnection) Connector.open(
                    "http://localhost:8080/WordPuzzleServer/GameManagerServlet?cmd=stopGame&gameId=" + id);

            c.setRequestMethod(HttpConnection.GET);

            try {
                is = c.openInputStream();
            } catch (IOException x) {
                x.printStackTrace();
            }

        } catch (IOException x) {
            x.printStackTrace();
        } finally {
            try {
                is.close();
                c.close();
            } catch (IOException x) {
                x.printStackTrace();
            }
        }
    }

    
}
